package com.flipdf2.serialize.ppml.element;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;
import com.flipdf2.serialize.ppml.AttributeType.PPMLWeight;

public class PPMLOccurrence {
	public static final String NAME = "OCCURRENCE";
	
	private PPMLView view;
	private List<PPMLTicketState> ticketStateList;
	
	private String name;
	private String environment;
	private String scope;
	private String overwriteMode;
	private int weight;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (view != null)
			root.addContent(view.toXML(defaultNamespace));
		
		if (ticketStateList != null && ticketStateList.size() > 0) {
			for (int i = 0, n = ticketStateList.size(); i < n; i++) {
				root.addContent(ticketStateList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (name != null) 
			root.setAttribute("Name", name);
		if (environment != null) 
			root.setAttribute("Environment", environment);
		if (scope != null) 
			root.setAttribute("Scope", scope);
		if (overwriteMode != null) 
			root.setAttribute("OverwriteMode", overwriteMode);
		if (PPMLWeight.isValid(weight)) 
			root.setAttribute("Weight", weight + "");
		
		return root;
	}
	
	public void check() throws PPMLException {
		if (name == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Name");
		}
	}

	public String getEnvironment() {
		return environment;
	}

	public void setEnvironment(String environment) {
		this.environment = environment;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOverwriteMode() {
		return overwriteMode;
	}

	public void setOverwriteMode(String overwriteMode) {
		this.overwriteMode = overwriteMode;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public List<PPMLTicketState> getTicketStateList() {
		return ticketStateList;
	}

	public void setTicketStateList(List<PPMLTicketState> ticketStateList) {
		this.ticketStateList = ticketStateList;
	}
	
	public void addTicketState(PPMLTicketState ticketState) {
		if (this.ticketStateList == null)
			this.ticketStateList = new ArrayList<PPMLTicketState>();
		this.ticketStateList.add(ticketState);
	}

	public PPMLView getView() {
		return view;
	}

	public void setView(PPMLView view) {
		this.view = view;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}
	
}
